#include "PhDSimul.h"

#include "TrajSimul.h"

#include "PhDSave.h"

#include <QFile>
#include <QTextStream>
#include <QString>

PhDSimul::PhDSimul()
{

}

PhDSimul::~PhDSimul()
{

}

MultiLayer PhDSimul::solve(Clock clock,Output output,Pulse *field,Pulse *current,MultiLayer multiLayer,NewParameters params)
{
  QTextStream cout(stdout,QIODevice::WriteOnly);

  //creates another layer with reverse magnetization
  MultiLayer downMultiLayer=multiLayer;
  int layerNumber=multiLayer.getSolveLayer();
  MultiLayer mup=multiLayer;
  MultiLayer mdown=multiLayer;
  QString filename;
  Vector3 tempVector;
  tempVector=multiLayer[layerNumber].getMag();
  tempVector.mult(-1);
  downMultiLayer[layerNumber].setMag(tempVector);
  PhDSave().initSave(output);

  for(current->setLevel(params.getCurrent()["min"]);current->getLevel(clock.getTimeStamp())<=params.getCurrent()["max"];current->setLevel(current->getLevel(clock.getTimeStamp())+params.getCurrent()["step"]))
    {
      for(field->setLevel(params.getField()["min"]);field->getLevel(clock.getTimeStamp())<=params.getField()["max"];field->setLevel(field->getLevel(clock.getTimeStamp())+params.getField()["step"]))
	{
	  int nrun;
	  Vector3 meanmag;
	  meanmag.setVector(0,0,0);
	  if(params.getTemperature()==0)
	    nrun=1;
	  for(int counter=0;counter<nrun;counter++)
	    {
	      output.setSave(false);
	      mup=TrajSimul().solve(clock,output,field,current,multiLayer,params);
	      meanmag.add(mup[layerNumber].getMag());
	    }
	  meanmag.mult(1/nrun);
	  mup[layerNumber].setMag(meanmag);
	  filename=output.getFileName();
	  filename.append("_up");
	  output.setPhDFile(filename);
	  PhDSave().save(output,mup[layerNumber]);
	  filename=output.getFileName();
	  filename.append("_down");
	  meanmag.setX(0);meanmag.setY(0);meanmag.setZ(0);
	  for(int counter=0;counter<nrun;counter++)
	    {	
	      output.setSave(false);
	      mdown=TrajSimul().solve(clock,output,field,current,downMultiLayer,params);
	      meanmag.add(mdown[layerNumber].getMag());
	    }
	  meanmag.mult(1/nrun);
	  mdown[layerNumber].setMag(meanmag);
	  output.setPhDFile(filename);
	  PhDSave().save(output,mdown[layerNumber]);
	  filename=output.getFileName();
	  filename.append("_diff");
	  output.setPhDFile(filename);
	  MultiLayer mtemp=mdown;
	  tempVector=mtemp[layerNumber].getMag();
	  tempVector.mult(-1);
	  tempVector.add(mup[layerNumber].getMag());
	  mtemp[layerNumber].setMag(tempVector);
	  PhDSave().save(output,mtemp[layerNumber]);
	  filename=output.getFileName();
	  filename.append("_sum");
	  output.setPhDFile(filename);
	  tempVector=mdown[layerNumber].getMag();
	  tempVector.add(mup[layerNumber].getMag());
	  tempVector.mult(0.5);
	  mup[layerNumber].setMag(tempVector);
	  PhDSave().save(output,mup[layerNumber]);
	}

      //to go to a new row
      filename=output.getFileName();
      filename.append("_up");
      output.setPhDFile(filename);     
      PhDSave().save(output,"\n");
      filename=output.getFileName();
      filename.append("_down");
      output.setPhDFile(filename);      
      PhDSave().save(output,"\n");
      filename=output.getFileName();
      filename.append("_diff");
      output.setPhDFile(filename);
      PhDSave().save(output,"\n");
      filename=output.getFileName();
      filename.append("_sum");
      output.setPhDFile(filename);
      PhDSave().save(output,"\n");
    }
  return multiLayer;
}

